package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.mapper.TContractMapper;
import com.esyoga.yoyoga.mapper.TGymMapper;
import com.esyoga.yoyoga.mapper.TStudentMapper;
import com.esyoga.yoyoga.mapper.TTrainerMapper;
import com.esyoga.yoyoga.pojo.DTO.ContractDTO;
import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.service.ContractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("contractService")
@Transactional
public class ContractServiceImpl implements ContractService {

    @Resource
    TContractMapper tContractMapper;
    @Resource
    TContractExample tContractExample;
    @Resource
    TStudentMapper tStudentMapper;
    @Resource
    TTrainerMapper tTrainerMapper;
    @Resource
    TGymMapper tGymMapper;



    @Override
    public List<TContract> selectContractBySidAndCstate(Integer sId, int cState) {
        tContractExample.clear();
        TContractExample.Criteria criteria = tContractExample.createCriteria();
        criteria.andCSIdEqualTo(sId);
        criteria.andCStateEqualTo(cState);
        List<TContract> tContracts= tContractMapper.selectByExample(tContractExample);
        return tContracts;
    }

    /**
     * 查询合同信息ContractDTO
     * @param contractList
     * @return
     */
    @Override
    public List<ContractDTO> selectContractDTO(List<TContract> contractList) {
        List<ContractDTO> contractDTOList = new ArrayList<>();

        for(TContract contract : contractList){
            TStudent student = tStudentMapper.selectByPrimaryKey(contract.getcSId());
            TGym gym = tGymMapper.selectByPrimaryKey(contract.getcGId());
            TTrainer trainer = tTrainerMapper.selectByPrimaryKey(contract.getcTId());
            ContractDTO contractDTO = new ContractDTO(trainer,student,gym,contract);
            contractDTOList.add(contractDTO);
        }
        return contractDTOList;
    }

    @Override
    public List<TContract> selectContractByTidAndCstate(Integer tId, int cState) {
        tContractExample.clear();
        TContractExample.Criteria criteria = tContractExample.createCriteria();
        criteria.andCTIdEqualTo(tId);
        criteria.andCStateEqualTo(cState);
        List<TContract> tContracts= tContractMapper.selectByExample(tContractExample);
        return tContracts;
    }

    @Override
    public int insertContract(TContract tContract) {

        return tContractMapper.insert(tContract);
    }

    @Override
    public int updateContract(TContract tContract) {

        return tContractMapper.updateByPrimaryKeySelective(tContract);
    }


    /**
     * 创建合约:
     *  1、先支付保证金，从账户余额里进行扣
     *  2、如果余额够则insert合约，如果不够则不insert
     * @param tContract
     * @return
     */
    @Override
    public Map<String, Object> createContract(TContract tContract) {
        Map<String, Object> result = new HashMap<>();
        //1、检查并更新用户余额
        BigDecimal margin = tContract.getcBanlance();
        //查询账户余额
        TStudent student = tStudentMapper.selectByPrimaryKey(tContract.getcSId());
        BigDecimal stuBalance  = student.getsBalance();
        if(margin.compareTo(stuBalance)>0){
            result.put("result","fail");
            result.put("errorCode","001");
            result.put("reason","用户余额不足");
            return result;

        }
        //更新用户余额
        TStudent stuUpdate  = new TStudent();
        stuUpdate.setsId(tContract.getcSId());
        stuUpdate.setsBalance(stuBalance.subtract(margin));
        if(tStudentMapper.updateByPrimaryKeySelective(stuUpdate)<1){
            result.put("result","fail");
            result.put("errorCode","002");
            result.put("reason","更新用户余额失败");
            return result;
        }
        //2、插入合约
        if(tContractMapper.insert(tContract)<1){
            result.put("result","fail");
            result.put("errorCode","003");
            result.put("reason","插入合约失败");
            return result;
        };
        result.put("result","success");
        return result;
    }

    @Override
    public TContract selectByCId(Integer cId) {
        return tContractMapper.selectByPrimaryKey(cId);
    }


    @Override
    public List<TContract> selectBysIdAndGIdAndTIdAndState(Integer sId, Integer gId, Integer tId, List<Integer> stateList) {
        tContractExample.clear();
        TContractExample.Criteria criteria = tContractExample.createCriteria();
        criteria.andCSIdEqualTo(sId).andCTIdEqualTo(tId).andCGIdEqualTo(gId).andCStateIn(stateList);
        return tContractMapper.selectByExample(tContractExample);
    }
}
